Write a program in C++ to swap two numbers using call by value

Assignment 7

Write a program in C++ to swap two numbers using call by value.

Program Code with explanation

1.          #include <iostream>

2.          using namespace std;

3.          void swap(int a, int b)

4.          {

5.                      int c;

6.                      c = a ;

7.                      a = b ;

8.                      b = c;

9.                      cout<<"Number After swap \n";

10.                   cout<<" a = "<< a <<" \t b = "<< b << endl;

11.       }

12.       int main()

13.       {

14.                   int a, b;

15.                   cout<<" Enter two number to swap using call by value \n";

16.                   cout<<" Enter first number  ";

17.                   cin>>a;

18.                   cout<<" Enter second number  ";

19.                   cin>>b;

20.                   cout<<"Number Before swap \n ";

21.                   cout<<"a = " << a << "\t b = "<< b << endl;

22.                   swap(a, b);

23.                   cout<<"Display values after swap function call completed \n ";

24.                   cout<<"a = " << a << "\t b = "<< b << endl;

25.                   return 0;

26.       }

Explanation of Code:

Line No. 1 and 2: include the Library file and stander input – output functions.

Line No. 3 to 11:  User defined function of call by value. Swap function has two arguments. Call statement of swap function at Line No 22. The function call statement contains the actual parameter, they are copied to the formal parameter of the called function.

            Values of actual parameter are copy in formal parameter. Formal Parameters can be used as local variables. In the swap function, formal parameters create local variables. These local variables will be swap using third variable. This means that there will be no change in the actual parameter.

Line No. 12: main function header.

Line No. 13 and 24: Begin and end of main function respectively.

Line No. 14: Create two integer variables.

Line No. 15 to 19: Accept two integer variables using appropriate massage.

Line No. 20 to 21: Display the values of variables before swap.

Line No. 22: call swap function using two parameters. These two parameters are actual parameter. Values of actual parameter copy into formal parameter at line no 3. Change the values of formal parameter are not affected the actual parameter.

            We swap formal parameters but no change in actual parameter. After function call completed.          

Line No. 23 and 24: There was no change in the actual parameters. Displayed variables to check it.

Line No. 25: return statement appropriate with main function return data type.

Program Code for run:

#include <iostream>

using namespace std;

void swap(int a, int b)

{

            int temp;

            temp = a ;

            a = b ;

            b = temp;

            cout<<"Number After swap \n";

            cout<<"a = "<< a << " \t b = "<< b << endl;

}

int main()

{

            int a, b;

            cout<<"Enter two number to swap using call by value\n";

            cout<<"Enter first number ";

            cin>>a;

            cout<<"Enter second number ";

            cin>>b;

            cout<<"Number Before swap \n";

            cout<<" a = " << a << "\t b = " << b <<endl;

            swap(a, b);

cout<<"Display values after swap function call completed \n ";

                    cout<<"a = " << a << "\t b = "<< b << endl;

            return 0;

}

Output of Program:

Enter two number to swap using call by value

Enter first number 10

Enter second number 20

Number Before swap

 a = 10            b = 20

Number After swap

a = 20              b = 10

Display values after swap function call completed

                      a = 10            b = 20

Comments